package com.example.liaotian.Fragment.UpFragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.liaotian.Adapter.TraAdapter;
import com.example.liaotian.Bean.Tra;
import com.example.liaotian.R;
import com.example.liaotian.activity.PushTraContentActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**跳蚤市场页面
 * A simple {@link Fragment} subclass.
 */
public class TransactionFragment extends Fragment {
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<Tra> data;//存放帖子的集合
    private TraAdapter traAdapter;
    private FloatingActionButton add,addup,addcontent;
    private PopupWindow pop;
    private RelativeLayout rvl;
    private View view1;


    public TransactionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tra, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        view1 = getLayoutInflater().inflate(R.layout.pop_item,null);

        initView();
        //初始化Bmob
        Bmob.initialize(getActivity(),"320a371a14fd5a1f1820feeeeb04e264");
        initData();
        Refresh();//fragment加载页面的时候就刷新一次
        initClick();
    }



    /**
     * 设置数据
     */
    private void initData() {
        //设置下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Refresh();
            }
        });

    }

    /**
     * 刷新的方法
     */
    private void Refresh(){
        BmobQuery<Tra> postBmobQuery = new BmobQuery<>();
        postBmobQuery.setLimit(1000).order("-createdAt");

//        postBmobQuery.order("-createdAt");//按时间从现在到以前查询
//        postBmobQuery.setLimit(1000);//限制为1000
        postBmobQuery.findObjects(new FindListener<Tra>() {
            @Override
            public void done(List<Tra> list, BmobException e) {
                swipeRefreshLayout.setRefreshing(false);//拿到数据之后就停止刷新动画
                //如果没有异常,就把数据放进集合中，并把数据填进recyclerview
                if (e == null) {
                    data = list;//把拿到的数据放进我们自己的集合
                    traAdapter = new TraAdapter(getActivity(),data);//实例化适配器对象并放入数据
                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//设置recyclerview为上下滑动
                    recyclerView.setAdapter(traAdapter);//把填好数据的适配器放进recyclerview
                }else {//如果有异常弹出一个提示
                    swipeRefreshLayout.setRefreshing(false);
                    Toast.makeText(getActivity(),"获取数据失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 绑定数据
     */
    private void initView() {
        recyclerView = getView().findViewById(R.id.re);
        swipeRefreshLayout = getView().findViewById(R.id.sw);
        add = getView().findViewById(R.id.add);
        addup = view1.findViewById(R.id.addup);
        addcontent = view1.findViewById(R.id.addcontent);
        rvl = getView().findViewById(R.id.rvl);



    }
    private void initClick() {

        add.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("RestrictedApi")
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), PushTraContentActivity.class));
//                pop = new PopupWindow(view1,view1.getWidth(),520,true);
//                pop.setOutsideTouchable(true);
//                pop.setFocusable(true);
//                pop.showAtLocation(rvl, Gravity.GRAVITY,100,200);

            }
        });

        addup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Refresh();
            }
        });

        addcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), PushTraContentActivity.class));
    }
});

    }

}
